<?php
namespace App\Services\Push;

use App\Services\Push\Apans;
use App\Services\Push\Gcm;
use App\Services\Push\MsgTypeConvert;
use Illuminate\Support\Facades\DB;

	class PushAppMsg {
		protected $user_id = null;
		protected $msg = null;
		protected $device_type = 0;
		protected $token = null;
		protected $lang = null;
		public function __construct($user_id, $msg){
			$this->user_id = $user_id;
			$this->msg = $msg;

			if (!isset($this->msg['title_t'])) {
				$this->msg['title_t'] = '';
			}
			if (!isset($this->msg['title_e'])) {
				$this->msg['title_e'] = '';
			}
			if (!isset($this->msg['content_e'])) {
				$this->msg['content_e'] = '';
			}
			if (!isset($this->msg['content_t'])) {
				$this->msg['content_t'] = '';
			}

			$this->getDeviceInfo();
		}

		protected function getDeviceInfo(){
			$sql = 'SELECT * FROM fenqi_device_token WHERE user_id=?';
			try{
				$device_info = DB::selectOne($sql, [$this->user_id]);
				if (isset($device_info->token) && $device_info->token) {
					$this->token = $device_info->token;
					$this->lang = $device_info->lang;
					$this->device_type = $device_info->device_type == 1 ?  $device_info->device_type : 0;
				}
			} catch (\Exception $e) {
				// throw $e;
				file_put_contents('error_log.log', date('Y-m-d H:i:s') . ': ' . $e->getMessage() . PHP_EOL, FILE_APPEND);
				return false;
			} catch (\Throwable $e) {
				file_put_contents('error_log.log', date('Y-m-d H:i:s') . ': ' . $e->getMessage() . PHP_EOL, FILE_APPEND);
				// throw $e;
				return false;
			}
		}

		protected function getGcmMsg(){
			$data_msg = array();
			$data_msg['msg_id'] = $this->msg['id'];
			$data_msg['type'] = MsgTypeConvert::intToString(intval($this->msg['app_type']));
			$data_msg['sub_type'] = $this->msg['app_sub_type'];
			$data_msg['title'] = $this->lang == 1 ? $this->msg['title'] : ($this->lang == 3 ? $this->msg['title_e'] : $this->msg['title_t']);
			$tmp_msg = $this->lang == 1 ? $this->msg['content'] : ($this->lang == 3 ? $this->msg['content_e'] : $this->msg['content_t']);
			$data_msg['msg'] = strip_tags($tmp_msg);

			$body = $this->lang == 1 ? $this->msg['content'] : ($this->lang == 3 ? $this->msg['content_e'] : $this->msg['content_t']);
			$body = strip_tags($body);
			return '{
				"registration_ids" : ' . json_encode(array($this->token)) . ',
				"notification" : {
					"body" : "' . $body . '",
					"title" : "' . $this->msg['title'] . '"
				},
				"data" : {
					"msg" : ' . json_encode($data_msg) . '
				}
			}';
		}

		protected function getApansMsg(){
			$data_msg = array();
			$data_msg['msg_id'] = $this->msg['id'];
			$data_msg['type'] = MsgTypeConvert::intToString(intval($this->msg['app_type']));
			$data_msg['sub_type'] = $this->msg['app_sub_type'];
			$data_msg['title'] = $this->lang == 1 ? $this->msg['title'] : ($this->lang == 3 ? $this->msg['title_e'] : $this->msg['title_t']);
			$tmp_msg = $this->lang == 1 ? $this->msg['content'] : ($this->lang == 3 ? $this->msg['content_e'] : $this->msg['content_t']);
			$data_msg['msg'] = strip_tags($tmp_msg);

			$body = $this->lang == 1 ? $this->msg['content'] : ($this->lang == 3 ? $this->msg['content_e'] : $this->msg['content_t']);
			$body = strip_tags($body);
			return '{
				"token" : "' . $this->token . '",
				"title" : "' . $this->msg['title'] . '",
				"body" : "' . $body . '",
				"data" : {
					"msg" : ' . json_encode($data_msg) . '
				}
			}';
		}

		// 推送消息
		public function push(){
			if (!$this->token) {
				return;
			}
			$msg = $this->device_type == 1 ? $this->getApansMsg() : $this->getGcmMsg();
			$pusher = $this->device_type == 1 ? new Apans() : new Gcm();
			return $pusher->push($msg);
		}
	}